<template>
  <div class="wrapper">
    <img
      src="http://www.dell-lee.com/imgs/vue3/user.png"
      alt=""
      class="warpper__img"
    />
    <div class="wrapper__input">
      <input
        type="text"
        class="wrapper__input__content"
        placeholder="请输入手机号"
        v-model="username"
      />
    </div>
    <div class="wrapper__input">
      <input
        type="password"
        class="wrapper__input__content"
        placeholder="请输入密码"
        v-model="password"
      />
    </div>
    <div class="wrapper__login__button" @click="handelLogin">登录</div>
    <div class="wrapper__login__link" @click="handelLink">立即注册</div>
    <Toast v-if="show" :message="toastMessage" />
  </div>
</template>
<script>
import { useRouter } from 'vue-router'
import { reactive, toRefs } from 'vue'
import { post } from '../../utils/request.js'
import Toast, { useToastEffect } from '../../components/Toast.vue'
const useLoginEffect = showToast => {
  const router = useRouter()
  const data = reactive({
    username: '',
    password: ''
  })
  const handelLogin = async () => {
    try {
      const { username, password } = data
      if (username.trim().length === 0 || password.trim().length === 0) {
        showToast('用户名或者密码为空')
        return
      }
      const reslut = await post('api/user/login', {
        username: data.username,
        password: data.password
      })
      if (reslut?.errno === 0) {
        localStorage.setItem('isLogin', true)
        router.push({ name: 'Home' })
      } else {
        showToast('登录失败')
      }
    } catch (error) {
      showToast('请求失败')
    }
  }
  const { username, password } = toRefs(data)
  return { handelLogin, username, password }
}
const useRegsiterEffect = () => {
  const router = useRouter()
  const handelLink = () => {
    router.push('/register')
  }
  return { handelLink }
}
export default {
  name: 'Login',
  components: {
    Toast
  },
  setup(props) {
    const { showToast, show, toastMessage } = useToastEffect()
    const { handelLogin, username, password } = useLoginEffect(showToast)
    const { handelLink } = useRegsiterEffect()
    return { username, password, handelLogin, handelLink, show, toastMessage }
  }
}
</script>
<style lang="scss" scoped>
@import '../../style/common.scss';

.wrapper {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  padding: 0 0.4rem;
  box-sizing: border-box;
  .warpper__img {
    display: block;
    width: 0.66rem;
    height: 0.66rem;
    margin: 0 auto;
    margin-bottom: 0.4rem;
  }
  .wrapper__input {
    width: 100%;
    height: 0.48rem;
    background: #f9f9f9;
    border: 0.01rem solid rgba(0, 0, 0, 0.1);
    border-radius: 0.06rem;
    padding-left: 0.16rem;
    padding-right: 0.16rem;
    box-sizing: border-box;
    margin-bottom: 0.16rem;
    input {
      width: 100%;
      height: 100%;
      background: #f9f9f9;
      border: 0;
      outline: 0;
      font-size: 0.16rem;
      color: $content-notice-fontColor;
    }
  }
  .wrapper__login__button {
    width: 100%;
    background: #0091ff;
    box-shadow: 0 0.04rem 0.08rem 0 rgba(0, 145, 255, 0.32);
    border-radius: 0.04rem;
    line-height: 0.48rem;
    font-size: 0.16rem;
    color: $bgColor;
    text-align: center;
    margin-top: 0.32rem;
    margin-bottom: 0.16rem;
    letter-spacing: 0.05rem;
    box-sizing: border-box;
  }
  .wrapper__login__link {
    text-align: center;
    font-size: 0.14rem;
    color: $content-notice-fontColor;
  }
}
</style>
